package com.example.photograph.service.impl;

import com.example.photograph.common.core.text.Convert;
import com.example.photograph.common.utils.DateUtils;
import com.example.photograph.domain.CascadeValue;
import com.example.photograph.domain.CxSelect;
import com.example.photograph.domain.SysSchool;
import com.example.photograph.mapper.SysSchoolMapper;
import com.example.photograph.service.ISysSchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 学院管理Service业务层处理
 *
 * @author ruoyi
 * @date 2021-05-01
 */
@Service("school")
public class SysSchoolServiceImpl implements ISysSchoolService {
    @Autowired
    private SysSchoolMapper sysSchoolMapper;

    /**
     * 查询学院管理
     *
     * @param id 学院管理ID
     * @return 学院管理
     */
    @Override
    public SysSchool selectSysSchoolById(Long id) {
        return sysSchoolMapper.selectSysSchoolById(id);
    }

    @Override
    public List<SysSchool> selectSysSchoolListAll() {
        return sysSchoolMapper.selectSysSchoolList(new SysSchool());
    }

    /**
     * 查询学院管理列表
     *
     * @param sysSchool 学院管理
     * @return 学院管理
     */
    @Override
    public List<SysSchool> selectSysSchoolList(SysSchool sysSchool) {
        return sysSchoolMapper.selectSysSchoolList(sysSchool);
    }

    /**
     * 新增学院管理
     *
     * @param sysSchool 学院管理
     * @return 结果
     */
    @Override
    public int insertSysSchool(SysSchool sysSchool) {
        sysSchool.setCreateTime(DateUtils.getNowDate());
        return sysSchoolMapper.insertSysSchool(sysSchool);
    }

    /**
     * 修改学院管理
     *
     * @param sysSchool 学院管理
     * @return 结果
     */
    @Override
    public int updateSysSchool(SysSchool sysSchool) {
        sysSchool.setUpdateTime(DateUtils.getNowDate());
        return sysSchoolMapper.updateSysSchool(sysSchool);
    }

    /**
     * 删除学院管理对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysSchoolByIds(String ids) {
        return sysSchoolMapper.deleteSysSchoolByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除学院管理信息
     *
     * @param id 学院管理ID
     * @return 结果
     */
    @Override
    public int deleteSysSchoolById(Long id) {
        return sysSchoolMapper.deleteSysSchoolById(id);
    }

    @Override
    public List<CxSelect> listCascade() {
        List<CascadeValue> cascadeValues = sysSchoolMapper.listCascade();

        Map<Long, List<CascadeValue>> collect = cascadeValues.stream().collect(Collectors.toMap(p -> p.getSchoolId(), p -> {
            ArrayList<CascadeValue> objects = new ArrayList<>();
            objects.add(p);
            return objects;
        }, (oldVal, newVal) -> {
            oldVal.addAll(newVal);
            return oldVal;
        }));
        ArrayList<CxSelect> roots = new ArrayList<>();
        for (Map.Entry<Long, List<CascadeValue>> longListEntry : collect.entrySet()) {

            Long key = longListEntry.getKey();
            List<CascadeValue> value = longListEntry.getValue();

            CxSelect root = new CxSelect();
            root.setN(value.get(0).getSchoolName());
            root.setV(String.valueOf(key));
            ArrayList<CxSelect> objects = new ArrayList<>();
            for (CascadeValue cascadeValue : value) {

                CxSelect son = new CxSelect();
                son.setN(cascadeValue.getMajoNmae());
                son.setV(String.valueOf(cascadeValue.getMajorId()));
                objects.add(son);
            }
            root.setS(objects);
            roots.add(root);
        }


        return roots;
    }
}
